﻿using MiniFox.Components;
using MiniFox.Data;
using MiniFox.Data.EntityFramework;
using MiniFox.Infrastructure;
using MiniFox.Platform.Applications.Auth;
using MiniFox.Utility;
using Platform.Core.Models;
using Platform.Core.Providers;

namespace Platform.Data
{
    [AutoResolve]
    public class PortletProvider : EntityDataProvider<PlatformDbContext>, IPortletProvider
    {
        const string REPO_NAME = "portlet";
        public Widget[] GetMyWidgets(SystemPrincipal principal)
        {
            var query = (from v in this.CurrentDbContext.VersionSet
                        join p in this.CurrentDbContext.PublishLog on v.PubId equals p.Id
                        join i in this.CurrentDbContext.VersionItem on p.VersionId equals i.Id
                        join uw in this.CurrentDbContext.UserWidget on v.Name equals uw.WidgetName
                        orderby uw.Ordinal ascending
                        where v.RepoName == REPO_NAME && v.PubId != null && uw.UserId == principal.GlobalID
                        select new
                        {
                            v.Name,
                            //v.RepoName,
                            //v.PubId,
                            //v.Description,
                            //v.UpdateTime,
                            p.PublisherId,
                            p.PublisherName,
                            p.PublishTime,
                            VersionId = i.Id,
                            i.Content,
                            i.AuthorId,
                            i.AuthorName,
                            i.Comment
                        }).ToArray();

            var result = query.Select(w => {
                dynamic content = w.Content.ToJsonObject();
                return new Widget
                {
                    Name = w.Name,
                    Publisher = w.PublisherName,
                    PublishTime = w.PublishTime,

                    Title = content.title,
                    Icon = content.icon,
                    ColSpan = content.colSpan,
                    RowSpan = content.rowSpan,
                    ClassName = content.classes,
                    ShowHeader = content.showHeader,
                    ShowFooter = content.showFooter,
                    EnterUrl = content.enterUri,
                    Template = content.template,
                    Script = content.script,
                    Description = content.description,
                    Schedule = new Schedule { Delay = content.delay, Interval = content.interval },
                    DataSource = new Service
                    {
                        Url = content.dataUri,
                        Method = content.method,
                        Headers = content.headers,
                        Params = content.@params,
                        Data = content.data == null ? null : content.data
                    }
                };
            });

            return result.ToArray();
        }

        public ResultSet<Widget> Query(SystemPrincipal principal, PagedQueryObject<WidgetQuery> request)
        {
            var query = (from v in this.CurrentDbContext.VersionSet
                         join p in this.CurrentDbContext.PublishLog on v.PubId equals p.Id
                         join i in this.CurrentDbContext.VersionItem on p.VersionId equals i.Id
                         join u in this.CurrentDbContext.SysUser on p.PublisherId equals u.UserId
                         where v.RepoName == REPO_NAME
                         select new
                         {
                             v.Name,
                             p.PublisherName,
                             p.PublishTime,
                             Content = i.Content.ToJsonObject()
                         });
            var resultSet = this.PagedQueryExecute(query, request);
            var list = resultSet.Select(v => v);

            PagedResultSet<Widget> result = new PagedResultSet<Widget>(resultSet.PageIndex, resultSet.PageSize);
            result.Data = list.Select(w => {
                dynamic content = w.Content;
                return new Widget
                {
                    Name = w.Name,
                    Publisher = w.PublisherName,
                    PublishTime = w.PublishTime,

                    Title = content.title,
                    Icon = content.icon,
                    ColSpan = content.colSpan,
                    RowSpan = content.rowSpan,
                    ClassName = content.classes,
                    ShowHeader = content.showHeader,
                    ShowFooter = content.showFooter,
                    EnterUrl = content.enterUri,
                    Template = content.template,
                    Script = content.script,
                    Description = content.description,
                    Schedule = new Schedule { Delay = content.delay, Interval = content.interval },
                    DataSource = new Service
                    {
                        Url = content.dataUri,
                        Method = content.method,
                        Headers = content.headers,
                        Params = content.@params,
                        Data = content.data == null ? null : content.data
                    }
                };
            });
            return result;
        }

        public Widget[] Save(SystemPrincipal principal, params string[] widgetNames)
        {
            var list = this.CurrentDbContext.UserWidget.Where(u=>u.UserId == principal.GlobalID).ToArray();
            this.Delete(list);
            var arr = new List<UserWidget>();
            int ordinal = 0;
            foreach (var name in widgetNames)
            {
                arr.Add(new UserWidget
                {
                    UserId = principal.GlobalID,
                    WidgetName = name,
                    Ordinal = ordinal++
                });
            }
            this.Insert(arr.ToArray());

            return this.GetMyWidgets(principal);
        }
    }
}
